package com.torry.workflow.common.util;

import com.torry.workflow.common.consts.AuthConstant;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class JwtUtil {

    /**
     * token 加密密钥
     */
   // private static String token_secret = "personal_blog_token";

    //private static long expiration = 1000 * 60 * 60 * 24 * 7;
    //private static long expiration = 1000 * 5;

    /**
     *
     */
    /**
     * @param username 用户名
     */
    /*public static String sign(String username) {
        Date expireTime = new Date(System.currentTimeMillis() + AuthConstant.TOKEN_EXPIRATION);

        Map<String, Object> claims = new HashMap<>();
        claims.put("username", username);
        claims.put("date", new Date(System.currentTimeMillis()).toString());
        return Jwts.builder()
                //.setSubject(username)
                .setClaims(claims)
                .setExpiration(expireTime)
                .signWith(SignatureAlgorithm.HS512, AuthConstant.JWT_TOKEN_SECRET)
                .compact();
    }*/

    /**
     * @param username 用户名, 用户类型
     */
    public static String sign(String username, Integer id) {
        Date expireTime = new Date(System.currentTimeMillis() + AuthConstant.TOKEN_EXPIRATION);

        Map<String, Object> claims = new HashMap<>();
        claims.put("username", username);
        claims.put("id", id);
        //claims.put("userType", userType);
        claims.put("date", new Date(System.currentTimeMillis()).toString());
        return Jwts.builder()
                //.setSubject(username)
                .setClaims(claims)
                .setExpiration(expireTime)
                .signWith(SignatureAlgorithm.HS512, AuthConstant.JWT_TOKEN_SECRET)
                .compact();
    }


    //判断是否在是否在过期时间之前
    public static Boolean isTokenExpired(String token) {
        try {
            Claims claims = Jwts.parser().setSigningKey(AuthConstant.JWT_TOKEN_SECRET).parseClaimsJws(token).getBody();
            Date expiration = claims.getExpiration();
            return expiration.before(new Date());
        } catch (ExpiredJwtException e) {
            return true;
        }
    }

    //获得的token中得到用户
    public static String getUsernameFromToken(String token) {

        Claims claims = Jwts.parser().setSigningKey(AuthConstant.JWT_TOKEN_SECRET).parseClaimsJws(token).getBody();
        return claims.get("username", String.class);
    }

    //获得的token中得到用户
    public static Integer getUserIdFromToken(String token) {

        Claims claims = Jwts.parser().setSigningKey(AuthConstant.JWT_TOKEN_SECRET).parseClaimsJws(token).getBody();
        return claims.get("id", Integer.class);
    }

    //获得的token中得到用户
   /* public static int getUserTypeFromToken(String token) {

        Claims claims = Jwts.parser().setSigningKey(AuthConstant.JWT_TOKEN_SECRET).parseClaimsJws(token).getBody();
        return claims.get("userType", Integer.class);
    }*/


    public static void main(String[] args) throws InterruptedException {
        String token = JwtUtil.sign("admin",1);
        System.out.println(token);
        System.out.println(JwtUtil.getUsernameFromToken(token));
        //System.out.println(JwtUtil.isTokenExpired(token));
        //Thread.sleep(10000);
        //System.out.println(JwtUtil.isTokenExpired(token));
    }

}
